<?php

function p($var){
	if(DEBUG){
		echo "<hr>";
		echo "var_dump ";
		echo "<hr><pre>";
		var_dump($var);
		echo "<hr></pre>";
	}
}

function alert($str)
{
	echo "<script>alert('$str')</script>";
}

/*
	@param 名称
	@param 默认值(默认false)
	@param 过滤接收到的post数据
*/
function getvalue($type, $name, $default=false, $filter=null)
{
	$arr = array();
	if($type === 'post') $arr = $_POST;
	else if($type === 'get') $arr = $_GET;
	if(isset($arr[$name])){
		$value = $arr[$name];
		if(!is_null($filter))
			$value = $filter($value);
		return $value;
			
	}
	else{
		return $default;
	}
}

function jump($url){
	header('Location:'.$url);
	exit(0);
}

/**
 * 获取$dir目录下的music的列表
 * 返回包含文件列表的数组
 */
function getMusicName($dir){
	$path = MVC . '/' . $dir;
	$fileName = [];
	if(is_dir($path)){
		$opdir = opendir($path);
		if($opdir){
			while($file = readdir($opdir)){
				$type = fileType($path .'/'. $file);
				if($type == "file"){
					$fileName[] = substr($file, 0, strlen($file)-4);
				}
			}
			closedir($opdir);
		}
	}
	$files = [];
	foreach($fileName as $index => $name){
		$files[$index]['name'] = $name;
		$files[$index]['path'] = $dir .'/' . $name . '.mp3';
	}
	return $files;
}
/**
 *	取得$dir目录下的所有文件路径
 */
function getFilepath($dir){
	$path = MVC . '/file/' . $dir;
	$filePath = [];
	if(is_dir($path)){
		$opdir = opendir($path);
		if($opdir){
			while($file = readdir($opdir)){
				$type = filetype($path . '/' . $file);
				if($type == 'file')
					$filePath[] = '/file/'.$dir . '/' . $file;
			}
			closedir($opdir);
		}
	}
	return $filePath;
}